using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SkillObject_Domain : SkillObject_Base
{
    private Skill_DomainExpansion domainManager;

    private float duration = 3f;
    private float expandSpeed = 3f;
    private Vector2 targetScale;
    private float slowPercent;
    private bool isShrink;

    protected override void Update()
    {
        base.Update();
        HandleDomainScaling();
    }
    public void SetUpDomain(Skill_DomainExpansion domainManager)
    {
        this.domainManager = domainManager;
        damageScaleData = domainManager.damageScaleData;
        player = domainManager.player;

        duration = domainManager.GetDomainDuration();
        slowPercent = domainManager.GetSlowdownPercent();
        expandSpeed = domainManager.expandSpeed;
        targetScale = Vector2.one * domainManager.domainExpandFactor;

        Invoke(nameof(ShrinkDomain), duration);
    }

    private void HandleDomainScaling()
    {
        float sizeDifference = Mathf.Abs(transform.localScale.x - targetScale.x);
        if (sizeDifference > .1f)
            transform.localScale = Vector2.Lerp(transform.localScale, targetScale, expandSpeed * Time.deltaTime);
        if (isShrink && sizeDifference < .1f)
            Destroy(gameObject);
    }
    private void ShrinkDomain()
    {
        isShrink = true;
        targetScale = Vector2.zero; 
        domainManager.ClearCollectedEnemy();
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        Enemy enemy = collision.GetComponent<Enemy>();
        if (enemy == null)
            return;
        domainManager.CollectEnemyInDomain(enemy);
        enemy.StartSlowdownEntityCo(duration, slowPercent, true);
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        Enemy enemy = collision.GetComponent<Enemy>();
        if (enemy == null)
            return;
        enemy.StopSlowdownEntityCo();
    }
}
